﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="MailBoxScripts.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.OutlookLive
{
    /// <summary>
    /// Name of scripts used for powershell commands.
    /// </summary>
    internal static class MailBoxScripts
    {
        /// <summary>
        /// Name of script used to add mailbox permission
        /// </summary>
        internal const string AddMailboxPermission = "Add-MailboxPermission -Identity {0} -User {1} -AccessRights fullaccess";

        /// <summary>
        /// Name of script used to add proxy email address to user
        /// </summary>
        internal const string AddProxyEmailAddressScript = @"$user = '{0}'
                                $mailbox = Get-Mailbox -identity $user
                                $mailbox.EmailAddresses += '{1}'
                                set-mailbox -identity $user -EmailAddresses $mailbox.EmailAddresses";

        /// <summary>
        /// Name of script used to create user mailbox
        /// </summary>
        internal const string CreateMailBoxScript = "new-mailbox -Name:'{0}' -Password:(ConvertTo-SecureString {1}  -AsPlainText –Force) -WindowsLiveID:{2} -FirstName:{3} -LastName:{4} -DisplayName:'{5}'";

        /// <summary>
        /// Name of script used to disable Mailbox
        /// </summary>
        internal const string DisableMailBoxScript = "Set-CASMailbox -identity {0} -OWAEnabled ${1} -ImapEnabled ${1} -PopEnabled ${1} -MAPIEnabled ${1}";
        
        /// <summary>
        /// Name of script used to get CASMailbox
        /// </summary>
        internal const string GetCASMailboxScript = "Get-CASMailbox -identity {0}";

        /// <summary>
        /// Name of script used to get mailboxes of all limits
        /// </summary>
        internal const string GetMailboxesOfAllSize = "Get-Mailbox | Get-MailboxStatistics";

        /// <summary>
        /// Name of script used to get mailboxes of certain limits
        /// </summary>
        internal const string GetMailboxesOfSpecifiedSize = @"function fn {
	        param ([ref]$integer, $size)

	        $startIndex = $size.indexof('(') + 1;
	        $str = $size.substring($startIndex, ($size.length - 7) - $startIndex);
	        $i = $str.Split(',');
   	        $temp = """";
   	        foreach($j in $i) {
 		        $temp = $temp + $j;
 	        }
 	        $integer.value = 1 * $temp
        }

        get-mailbox | get-mailboxstatistics | where { $temp = 0 ; fn -integer([ref]$temp) -size $_.TotalItemSize ; $temp {0} {1}; }";

        /// <summary>
        /// Name of script used to get mail box
        /// </summary>
        internal const string GetMailBoxScript = "Get-Mailbox -identity {0}";

        /// <summary>
        /// Name of script used to import accounts
        /// </summary>
        internal const string ImportAccountScript = @"$UserFile = Import-Csv '{0}'
                                    $Error.clear()
                                    foreach($user in $UserFile)
                                    {    
                                        $identity = $user.Name    
                                        $mailbox = Invoke-Command -ErrorAction SilentlyContinue -arg $identity {param ($identity) get-mailbox -identity $identity }
                                            
                                        if(![string]::IsNullOrEmpty($Error[0]))    
                                        {                
                                            # create mail box
                                            $result = Invoke-Command -ScriptBlock{ new-mailbox -Name $user.Name -Password (ConvertTo-SecureString $user.Password  -AsPlainText –Force) -WindowsLiveID $user.EmailAddress -FirstName $user.FirstName -LastName $user.LastName -DisplayName $user.DisplayName }        
                                        }
                                        else
                                        {   
                                            # just leave it        
                                        }
                                    }";

        /// <summary>
        /// Name of script used to redirect user's mail to another mailbox.
        /// </summary>
        internal const string RedirectMailScript = "New-InboxRule -Name '{0}' -Mailbox {1} -RedirectTo {2}";

        /// <summary>
        /// Name of script used to remove mailbox permissions
        /// </summary>
        internal const string RemoveMailboxPermissionScript = "Remove-MailboxPermission -Identity {0} -User {1} -AccessRights fullaccess";

        /// <summary>
        /// Name of script used to remove a rule
        /// </summary>
        internal const string RemoveRuleScript = "Remove-InboxRule -Identity '{0}'";

        /// <summary>
        /// Name of script used to reset password
        /// </summary>
        internal const string ResetPasswordScript = "Set-Mailbox -Identity:{0} -password:(ConvertTo-SecureString {1} -AsPlainText –Force)";

        /// <summary>
        /// Name of script used to block emails from sender
        /// </summary>
        internal const string SetRejectMessagesFromScript = "set-Mailbox -identity:{0} -RejectMessagesFrom:{1}";

        /// <summary>
        /// Name of script used to update primary email address
        /// </summary>
        internal const string UpdatePrimaryEmaillAddressScript = "set-mailbox {0} -EmailAddresses SMTP:{1}";
    }
}
